#ifndef HPGRAPHICSSCENE_H
#define HPGRAPHICSSCENE_H

#include <QtGui>
#include <QGraphicsScene>
#include <QGraphicsSceneEvent>
#include <QGraphicsLineItem>
#include <QGraphicsPathItem>
#include <QPointer>

#include "HPTools.h"

class HPGraphicsScene : public QGraphicsScene
{
    Q_OBJECT

public:

    enum InsertionMode
    {
        INSERT_LINE = 0,
        INSERT_ARC,
        INSERT_PATH,
        ERASE
    };

    explicit HPGraphicsScene( QSettings& rSettings, QObject *parent = 0);

    QColor pencilColor() const;
    void setPencilColor( QColor color );

    void setPenType( PenType penType );
    PenType penType() const;

    void setPenSize( int size );
    int penSize() const;

    void setPenColor( QColor color );
    QColor penColor() const;

    PenInfo penInfo() const;

    QPen displayPen() const;

    void setCanvasSize( QSize size );
    void setMMToPixelRatio( float ratio );
    float getMMToPixelRatio() const;

    QPen penInfoToQPen( PenInfo penInfo ) const;
    QString pointToRobot( QPointF point ) const;

    bool saveDrawing( QString filename );
    bool openDrawing( QString filename );

    bool saveScreenshot( QString filename );

    bool saveRobotControlFile( QString filename );

    void drawLine( QPixmap& rPixmap, QLineF line, QPen pen, float painterPathLength = 0 );
    void drawPainterPath( QPixmap& rPixmap, const QPainterPath& painterPath, PenInfo penInfo );

    void startPenPath( QPointF p1 );
    void continuePenPath( QPointF p2 );
    void linePenPath( QPointF p2 );
    void endPenPath();

    QPainterPath* storePenPath( const QPainterPath& painterPath, PenInfo penInfo );
    void unStorePenPath( QPainterPath* pPainterPath );

    void removeAllPenPath();

    int getPenLength() const;
    int getPenLengthMax() const;

    InsertionMode insertionMode() const;
    void setInsertionMode( InsertionMode insertionMode );

    void undo();
    void enableUndoOverview();
    void disableUndoOverview();

signals:
    void sigPenPathChanged();
    void sigChangeCanvasSizeRequested( QSize size );
    
public slots:
    void onSceneRectChanged ( const QRectF & rect );

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent);
    void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent);

private:

    QSettings&          m_settings;

    InsertionMode       m_insertionMode;

    QColor              m_pencilColor;

    PenInfo             m_penInfo;
    float               m_fPencilLengthMax;
    float               m_fMMToPixelRatio;
    float               m_fDrawingAccuracy;
    QSize               m_canvasSize;

    QPainterPath        m_painterPath;

    QPainterPath*       m_pPainterPathToUndo;
    PenInfo             m_penInfoToUndo;

    QGraphicsPixmapItem*m_pCurrentPixmapItem;
    QPixmap             m_currentPixmap;

    QGraphicsPixmapItem*m_pDrawingPixmapItem;
    QPixmap             m_drawingPixmap;

    QGraphicsPathItem*  m_pPathItemToInsert;
    QGraphicsPathItem*  m_pItemSelected;

    QMap<QPainterPath*,PenInfo> m_mPenInfos;
    QList<QPainterPath*>   m_lPainterPath;

    QPixmap             m_pixBrush;
    
};

#endif // HPGRAPHICSSCENE_H
